package com.cncytech.uiautomator_demo.proxy;

import android.os.Build;
import android.util.Log;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.reflect.TypeUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Objects;

public class ModuleProxy<T> {
    private static final String TAG = "ModuleProxy";


    public T getInstance(T target) {

        return (T) Proxy.newProxyInstance(ModuleProxy.class.getClassLoader(), target.getClass().getInterfaces(), (Object proxy, Method method, Object[] args) -> {
            Object result = null;
            String methodName = method.getName();
            try {
                StringBuffer strArgs = new StringBuffer();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    if (Objects.nonNull(args)) {
                        Arrays.stream(args).forEach(e -> {
                            if (!Objects.isNull(e)) {

                                // TODO: 2023/8/14  判断不是数组，简单转化，否则就按照数组
                                if (!TypeUtils.isArrayType(e.getClass())) {
                                    strArgs.append(ConvertUtils.convert(e, e.getClass()));

                                } else {
                                    strArgs.append(" : " + Arrays.toString((String[]) e));
                                }
                            }

                        });
                        Log.i(TAG, methodName + " :参数 = " + strArgs.toString());
                    }else{
                        Log.i(TAG, "methodName = " + methodName);
                    }
                }
                result = method.invoke(target, args);
                // Perform additional logic after method invocation
                // TODO: 2023/8/8 Based on the result, it should be either "red" or "green"
                if (result instanceof Boolean) {
                    if ((Boolean) result) {
                        Log.i(TAG, methodName + ".Pass");
                    } else {
                        Log.i(TAG, methodName + ".Failure");
                    }
                }
            } catch (Exception e) {
               e.printStackTrace();
            }
            return result;
        });
    }

    public T getInstance1(T target) {
        return (T) Proxy.newProxyInstance(ModuleProxy.class.getClassLoader(), target.getClass().getInterfaces(), new MyInvocationHandler(target));
    }

    class MyInvocationHandler<T> implements InvocationHandler {
        //invocationHandler持有的被代理对象
        T target;

        public MyInvocationHandler(T target) {
            this.target = target;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object result = null;
            String methodName = method.getName();
            StringBuffer strArgs = new StringBuffer();
            if (Objects.nonNull(args)) {
                for (Object e : args) {

                    // TODO: 2023/8/14  判断不是数组，简单转化，否则就按照数组
                    if (!TypeUtils.isArrayType(e.getClass())) {
                        strArgs.append(ConvertUtils.convert(e, e.getClass()));

                    } else {
                        strArgs.append(" : " + Arrays.toString((String[]) e));
                    }
                }
                Log.i(TAG, methodName + " :参数 = " + strArgs.toString());
            }

            result = method.invoke(target, args);

            // Perform additional logic after method invocation
            // TODO: 2023/8/8 Based on the result, it should be either "red" or "green"
            if (result instanceof Boolean) {
                if ((Boolean) result) {
                    Log.i(TAG, methodName + ".Pass");
                }
            } else if (Objects.isNull(result)) {
                Log.i(TAG, methodName + ".Failure");
            }
            return result;
        }
    }
}
